package com.androidme.app.examples;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import com.androidme.R;
import com.androidme.app.framework.base.AndroidMeListActivity;

import java.util.ArrayList;


public class TestListActivity extends AndroidMeListActivity<String> {
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.test_item_list, null, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.tv_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(getData(position));
        return convertView;
    }

    @Override
    protected void onPrepareData() {
        for (int i = 0; i < 30; i++) {
            addData("test" + i);
        }
        refresh();
    }

    @Override
    protected boolean hasMore() {
        return true;
    }

    @Override
    protected void getMore() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                int start = getDataCount();
                int length = 10;
                ArrayList<String> datas = new ArrayList<String>();
                for (int i = start; i < start + length; i++) {
                    datas.add("test" + i);
                }

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                addDatas(datas);
                refresh();
            }
        }).start();

    }

    @Override
    protected void addListFooter() {
        footView = layoutInflater.inflate(R.layout.test_more, null, false);
        footView.setVisibility(View.VISIBLE);
        listView.addFooterView(footView);
    }

    @Override
    protected void addListHeader() {
        headView = layoutInflater.inflate(R.layout.test_more, null, false);
        headView.setVisibility(View.VISIBLE);
        listView.addHeaderView(headView);
    }

    @Override
    protected boolean isDataSame(String t1, String t2) {
        return t1.trim().equals(t2.trim());
    }

    class ViewHolder {
        TextView textView;
    }
}